﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;

namespace Chenxin.Lib.Business.DAL
{
    /// <summary>
    /// 数据访问类t_Intention_DeliverLogs。
    /// </summary>
    public class t_Intention_DeliverLogs
    {
        public t_Intention_DeliverLogs()
        { }
        #region  成员方法

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public static void Add(Chenxin.Lib.Business.Entity.t_Intention_DeliverLogs model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into t_Intention_DeliverLogs(");
            strSql.Append("RecordId,IntentionId,IntentionCode,DeliverTime,DeliverEmpId,DeliverEmp,DeliverDepartId,DeliverDepart,DeliverCompanyId,DeliverCompany,DeliverCityId,DeliverCity,ReceiveEmpId,ReceiveEmp,ReceiveDepartId,ReceiveDepart,ReceiveCompanyId,ReceiveCompany,ReceiveCityId,ReceiveCity,Status)");

            strSql.Append(" values (");
            strSql.Append("@RecordId,@IntentionId,@IntentionCode,@DeliverTime,@DeliverEmpId,@DeliverEmp,@DeliverDepartId,@DeliverDepart,@DeliverCompanyId,@DeliverCompany,@DeliverCityId,@DeliverCity,@ReceiveEmpId,@ReceiveEmp,@ReceiveDepartId,@ReceiveDepart,@ReceiveCompanyId,@ReceiveCompany,@ReceiveCityId,@ReceiveCity,@Status)");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, model.RecordId);
            db.AddInParameter(dbCommand, "IntentionId", DbType.Guid, model.IntentionId);
            db.AddInParameter(dbCommand, "IntentionCode", DbType.AnsiString, model.IntentionCode);
            db.AddInParameter(dbCommand, "DeliverTime", DbType.DateTime, model.DeliverTime);
            db.AddInParameter(dbCommand, "DeliverEmpId", DbType.Guid, model.DeliverEmpId);
            db.AddInParameter(dbCommand, "DeliverEmp", DbType.String, model.DeliverEmp);
            db.AddInParameter(dbCommand, "DeliverDepartId", DbType.Guid, model.DeliverDepartId);
            db.AddInParameter(dbCommand, "DeliverDepart", DbType.AnsiString, model.DeliverDepart);
            db.AddInParameter(dbCommand, "DeliverCompanyId", DbType.Guid, model.DeliverCompanyId);
            db.AddInParameter(dbCommand, "DeliverCompany", DbType.AnsiString, model.DeliverCompany);
            db.AddInParameter(dbCommand, "DeliverCityId", DbType.Guid, model.DeliverCityId);
            db.AddInParameter(dbCommand, "DeliverCity", DbType.AnsiString, model.DeliverCity);
            db.AddInParameter(dbCommand, "ReceiveEmpId", DbType.Guid, model.ReceiveEmpId);
            db.AddInParameter(dbCommand, "ReceiveEmp", DbType.AnsiString, model.ReceiveEmp);
            db.AddInParameter(dbCommand, "ReceiveDepartId", DbType.Guid, model.ReceiveDepartId);
            db.AddInParameter(dbCommand, "ReceiveDepart", DbType.AnsiString, model.ReceiveDepart);
            db.AddInParameter(dbCommand, "ReceiveCompanyId", DbType.Guid, model.ReceiveCompanyId);
            db.AddInParameter(dbCommand, "ReceiveCompany", DbType.AnsiString, model.ReceiveCompany);
            db.AddInParameter(dbCommand, "ReceiveCityId", DbType.Guid, model.ReceiveCityId);
            db.AddInParameter(dbCommand, "ReceiveCity", DbType.AnsiString, model.ReceiveCity);
            db.AddInParameter(dbCommand, "Status", DbType.Int32, model.Status);
            db.ExecuteNonQuery(dbCommand);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public static void Update(Chenxin.Lib.Business.Entity.t_Intention_DeliverLogs model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update t_Intention_DeliverLogs set ");
            strSql.Append("IntentionId=@IntentionId,");
            strSql.Append("IntentionCode=@IntentionCode,");
            strSql.Append("DeliverTime=@DeliverTime,");
            strSql.Append("DeliverEmpId=@DeliverEmpId,");
            strSql.Append("DeliverEmp=@DeliverEmp,");
            strSql.Append("DeliverDepartId=@DeliverDepartId,");
            strSql.Append("DeliverDepart=@DeliverDepart,");
            strSql.Append("DeliverCompanyId=@DeliverCompanyId,");
            strSql.Append("DeliverCompany=@DeliverCompany,");
            strSql.Append("DeliverCityId=@DeliverCityId,");
            strSql.Append("DeliverCity=@DeliverCity,");
            strSql.Append("ReceiveEmpId=@ReceiveEmpId,");
            strSql.Append("ReceiveEmp=@ReceiveEmp,");
            strSql.Append("ReceiveDepartId=@ReceiveDepartId,");
            strSql.Append("ReceiveDepart=@ReceiveDepart,");
            strSql.Append("ReceiveCompanyId=@ReceiveCompanyId,");
            strSql.Append("ReceiveCompany=@ReceiveCompany,");
            strSql.Append("ReceiveCityId=@ReceiveCityId,");
            strSql.Append("ReceiveCity=@ReceiveCity,");
            strSql.Append("Status=@Status");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, model.RecordId);
            db.AddInParameter(dbCommand, "IntentionId", DbType.Guid, model.IntentionId);
            db.AddInParameter(dbCommand, "IntentionCode", DbType.AnsiString, model.IntentionCode);
            db.AddInParameter(dbCommand, "DeliverTime", DbType.DateTime, model.DeliverTime);
            db.AddInParameter(dbCommand, "DeliverEmpId", DbType.Guid, model.DeliverEmpId);
            db.AddInParameter(dbCommand, "DeliverEmp", DbType.String, model.DeliverEmp);
            db.AddInParameter(dbCommand, "DeliverDepartId", DbType.Guid, model.DeliverDepartId);
            db.AddInParameter(dbCommand, "DeliverDepart", DbType.AnsiString, model.DeliverDepart);
            db.AddInParameter(dbCommand, "DeliverCompanyId", DbType.Guid, model.DeliverCompanyId);
            db.AddInParameter(dbCommand, "DeliverCompany", DbType.AnsiString, model.DeliverCompany);
            db.AddInParameter(dbCommand, "DeliverCityId", DbType.Guid, model.DeliverCityId);
            db.AddInParameter(dbCommand, "DeliverCity", DbType.AnsiString, model.DeliverCity);
            db.AddInParameter(dbCommand, "ReceiveEmpId", DbType.Guid, model.ReceiveEmpId);
            db.AddInParameter(dbCommand, "ReceiveEmp", DbType.AnsiString, model.ReceiveEmp);
            db.AddInParameter(dbCommand, "ReceiveDepartId", DbType.Guid, model.ReceiveDepartId);
            db.AddInParameter(dbCommand, "ReceiveDepart", DbType.AnsiString, model.ReceiveDepart);
            db.AddInParameter(dbCommand, "ReceiveCompanyId", DbType.Guid, model.ReceiveCompanyId);
            db.AddInParameter(dbCommand, "ReceiveCompany", DbType.AnsiString, model.ReceiveCompany);
            db.AddInParameter(dbCommand, "ReceiveCityId", DbType.Guid, model.ReceiveCityId);
            db.AddInParameter(dbCommand, "ReceiveCity", DbType.AnsiString, model.ReceiveCity);
            db.AddInParameter(dbCommand, "Status", DbType.Int32, model.Status);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public static void Delete(Guid RecordId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from t_Intention_DeliverLogs ");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public static Chenxin.Lib.Business.Entity.t_Intention_DeliverLogs GetModel(Guid RecordId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,IntentionId,IntentionCode,DeliverTime,DeliverEmpId,DeliverEmp,DeliverDepartId,DeliverDepart,DeliverCompanyId,DeliverCompany,DeliverCityId,DeliverCity,ReceiveEmpId,ReceiveEmp,ReceiveDepartId,ReceiveDepart,ReceiveCompanyId,ReceiveCompany,ReceiveCityId,ReceiveCity,Status from t_Intention_DeliverLogs ");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business_Search");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            Chenxin.Lib.Business.Entity.t_Intention_DeliverLogs model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return model;
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public static string GetCode(Guid IntentionId)
        {
            string code = string.Empty;
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select IntentionCode from t_Intention_DeliverLogs ");
            strSql.AppendFormat(" where IntentionId='{0}' ", IntentionId);
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business_Search");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    code = dataReader["IntentionCode"].ToString();
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return code;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,IntentionId,IntentionCode,DeliverTime,DeliverEmpId,DeliverEmp,DeliverDepartId,DeliverDepart,DeliverCompanyId,DeliverCompany,DeliverCityId,DeliverCity,ReceiveEmpId,ReceiveEmp,ReceiveDepartId,ReceiveDepart,ReceiveCompanyId,ReceiveCompany,ReceiveCityId,ReceiveCity,Status ");
            strSql.Append(" FROM t_Intention_DeliverLogs ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business_Search");
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public static List<Chenxin.Lib.Business.Entity.t_Intention_DeliverLogs> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,IntentionId,IntentionCode,DeliverTime,DeliverEmpId,DeliverEmp,DeliverDepartId,DeliverDepart,DeliverCompanyId,DeliverCompany,DeliverCityId,DeliverCity,ReceiveEmpId,ReceiveEmp,ReceiveDepartId,ReceiveDepart,ReceiveCompanyId,ReceiveCompany,ReceiveCityId,ReceiveCity,Status ");
            strSql.Append(" FROM t_Intention_DeliverLogs ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<Chenxin.Lib.Business.Entity.t_Intention_DeliverLogs> list = new List<Chenxin.Lib.Business.Entity.t_Intention_DeliverLogs>();
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business_Search");
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public static Chenxin.Lib.Business.Entity.t_Intention_DeliverLogs ReaderBind(IDataReader dataReader)
        {
            Chenxin.Lib.Business.Entity.t_Intention_DeliverLogs model = new Chenxin.Lib.Business.Entity.t_Intention_DeliverLogs();
            object ojb;
            ojb = dataReader["RecordId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.RecordId = new Guid(ojb.ToString());
            }
            ojb = dataReader["IntentionId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.IntentionId = new Guid(ojb.ToString());
            }
            model.IntentionCode = dataReader["IntentionCode"].ToString();
            ojb = dataReader["DeliverTime"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.DeliverTime = (DateTime)ojb;
            }
            ojb = dataReader["DeliverEmpId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.DeliverEmpId = new Guid(ojb.ToString());
            }
            model.DeliverEmp = dataReader["DeliverEmp"].ToString();
            ojb = dataReader["DeliverDepartId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.DeliverDepartId = new Guid(ojb.ToString());
            }
            model.DeliverDepart = dataReader["DeliverDepart"].ToString();
            ojb = dataReader["DeliverCompanyId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.DeliverCompanyId = new Guid(ojb.ToString());
            }
            model.DeliverCompany = dataReader["DeliverCompany"].ToString();
            ojb = dataReader["DeliverCityId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.DeliverCityId = new Guid(ojb.ToString());
            }
            model.DeliverCity = dataReader["DeliverCity"].ToString();
            ojb = dataReader["ReceiveEmpId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ReceiveEmpId = new Guid(ojb.ToString());
            }
            model.ReceiveEmp = dataReader["ReceiveEmp"].ToString();
            ojb = dataReader["ReceiveDepartId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ReceiveDepartId = new Guid(ojb.ToString());
            }
            model.ReceiveDepart = dataReader["ReceiveDepart"].ToString();
            ojb = dataReader["ReceiveCompanyId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ReceiveCompanyId = new Guid(ojb.ToString());
            }
            model.ReceiveCompany = dataReader["ReceiveCompany"].ToString();
            ojb = dataReader["ReceiveCityId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ReceiveCityId = new Guid(ojb.ToString());
            }
            model.ReceiveCity = dataReader["ReceiveCity"].ToString();
            ojb = dataReader["Status"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.Status = (int)ojb;
            }
            return model;
        }

        #endregion  成员方法
    }
}